package com.example.enterprise

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.enterprise.adapter.EnterpriseAdapter
import com.example.enterprise.databinding.ActivityEnterpriseBinding
import com.example.enterprise.entity.EnterpriseEntityItem
import com.example.enterprise.vm.EnterpriseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route (path = "/enterprise/EnterpriseActivity")
class EnterpriseActivity : BaseMvvmActivity<ActivityEnterpriseBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_enterprise
    val enterpriseAdapter= EnterpriseAdapter()
    private val enterpriseViewModel:EnterpriseViewModel by viewModels()
    var name:String=""
    var list = mutableListOf<EnterpriseEntityItem>()
    override fun initView() {
        super.initView()
        mDataBinding.rv.layoutManager= LinearLayoutManager(this)
        mDataBinding.rv.adapter=enterpriseAdapter

        enterpriseViewModel.mSuccessLiveData.observe(this){
            enterpriseAdapter.data.clear()
            if (name.isNullOrEmpty()){
                enterpriseAdapter.data.addAll(it.data)
                list =enterpriseAdapter.data
            }else{
                enterpriseAdapter.data.addAll(it.data.filter { it.name.contains(name) })
            }
            enterpriseAdapter.notifyDataSetChanged()
        }
        enterpriseViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        enterpriseAdapter.setOnItemClickListener { adapter, view, position ->
            val enterpriseEntity = enterpriseAdapter.getItem(position)
            ARouter.getInstance().build("/enterprise/EnterpriseDetailActivity")
                .withParcelable("enterpriseEntity",enterpriseEntity)
                .navigation()
        }
        mDataBinding.add.setOnClickListener {
//            ARouter.getInstance().build("/enterprise/EnterpriseAddActivity").navigation()
            ARouter.getInstance().build( "/house/HouseAddActivity").navigation()
        }
        // 设置搜索框监听器
        setupSearchView()
    }

    override fun onResume() {
        super.onResume()
        enterpriseViewModel.getEnterprise()
    }

    private fun setupSearchView(){
        mDataBinding.search.setOnClickListener {
            val search = mDataBinding.et.text.toString().trim()
            if (search.isNotEmpty()){
                enterpriseAdapter.data.clear()
                list.forEach {
                    if (it.name.contains(search)){
                        enterpriseAdapter.data.add(it)
                    }
                }
            }else{
                enterpriseAdapter.data.clear()
                enterpriseAdapter.data.addAll(list)
            }
            enterpriseAdapter.notifyDataSetChanged()
        }
    }
}